//==================================================
// X_NC_template.ino
// 2015-02-04
//==================================================
// Nabton Client Generic Configuration
//==================================================

#define NFLOAT 4  // No of decimals i float value
#define SIDN  3   // No of SIDs
#define SID1 901
#define SID2 902
#define SID3 903
#define SID4 904
#define SID5 905
#define SID6 906
#define SID7 907
#define SID8 908

#define MAX_SID 10
#define MAX_ORDERS 100
#include <U8glib.h>
//U8GLIB_SSD1306_128X64 u8g(13, 11, 10, 9);// SW SPI protocol(4 pins): SCK = 13, MOSI = 11, CS = 10, A0 = 9	
U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NONE); // Small display I2C protocol (2 pins)
//U8GLIB_SH1106_128X64 u8g(U8G_I2C_OPT_NONE); // Large display
char dl[5][8],dm[5][8],dr[5][8];
char g_msg[120];
int g_sids[10] = {SIDN,SID1,SID2,SID3,SID4,SID5,SID6,SID7,SID8};
int g_on[MAX_ORDERS];    // Arduino - RPi interface par type
int g_value[MAX_ORDERS]; // Arduino - RPi interface par value
int g_sidHb[MAX_SID];
int g_sidHbUnit[MAX_SID];
int g_sidDelay[MAX_SID]; // Always in sec
int g_sidCountDown[MAX_SID];
#define PIN_LED_STATUS 13
#define COM_DELAY  200
// Arduino-RPi protocol
#define NABTON_DATA     1 
#define NABTON_DATA_ACK 2
#define NABTON_NODATA   3
#define NABTON_LOG      8 
#define NABTON_ERROR    9 
// Arduino Control Protocol
#define ON_HB_VAL        2
#define ON_HB_UNIT       3
#define ON_STEPPER_DIR   4
#define ON_STEPPER_STEPS 5
#define ON_STEPPER_SPEED 6

char g_errmsg[120];  // Error message
int g_error = 0;     // Error flag
char g_logmsg[120];  // Log message
int g_log = 0;       // Log flag
#define ERR_SID_NOT_FOUND  "SID_not_found"
#define ERR_UNSUPPORTED_MESSAGE "Unsupported_message"
#define ERR_NO_MESSAGE "No_message"
#define ERR_SID_MISMATCH "SID_Mismatch"
//=================================================
//
// D0 RX used for serial communication to server (Raspberry Pi)
// D1 TX used for serial communication to server (Raspberry Pi)
// D2
// D3 Message Pin (blink led)
// D4 IR Data
// D5
// D6 DIR Stepper
// D7 STEP Stepper
// D8 SLEEP Stepper
// D9 One Wire Data
// D10  RX Bluetooth device
// D11  TX Bluetooth device
// D12 
// D13
//
// A0
// A1
// A2
// A3
// A4 SDA I2C OLED
// A5 SCL I2C OLED

// MEGA
// D20 SDA I2C OLED
// D21 SCL I2C OLED
//=================================================
//==================================================
// Nabton Client Application Configuration
//==================================================
#include <OneWire.h>
#include <DallasTemperature.h>
//#include <SoftwareSerial.h>
//#include <IRremote.h>
#include <U8glib.h>

#define NC_NAME "D3.0"
//=================================================
// One Wire
//=================================================

#define ONE_WIRE_BUS 9
#define TEMPERATURE_PRECISION 12
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
DeviceAddress device[MAX_SID];
int nsensors = 0;

//=================================================
void NB_oledInit() 
//=================================================
{  
   // OLED
  if ( u8g.getMode() == U8G_MODE_R3G3B2 ) {
    u8g.setColorIndex(255);     // white
  }
  else if ( u8g.getMode() == U8G_MODE_GRAY2BIT ) {
    u8g.setColorIndex(3);         // max intensity
  }
  else if ( u8g.getMode() == U8G_MODE_BW ) {
    u8g.setColorIndex(1);         // pixel on
  }
  else if ( u8g.getMode() == U8G_MODE_HICOLOR ) {
    u8g.setHiColorByRGB(255,255,255);
  }  
}
  //=================================================
void NB_oledClear() 
//=================================================
{
    for(int i=1; i<=4; i++)
    {
      strcpy(dl[i],"-");
      strcpy(dm[i],"-");
      strcpy(dr[i],"-");
    }
}
  //=================================================
void NB_oledDraw() 
//=================================================
{
 u8g.firstPage();  
  do {
      // Horizontal pixels: 0 - 120
  // Vertical pixels:   0 -  63
  //u8g.setFont(u8g_font_6x10);
  u8g.setFont(u8g_font_unifont);
  //u8g.setFont(u8g_font_osb21);
  u8g.drawStr( 0, 10, dl[1]);
  u8g.drawStr( 0, 27, dl[2]);
  u8g.drawStr( 0, 45, dl[3]);
  u8g.drawStr( 0, 62, dl[4]);

  u8g.drawStr( 45, 10, dm[1]);
  u8g.drawStr( 45, 27, dm[2]);
  u8g.drawStr( 45, 45, dm[3]);
  u8g.drawStr( 45, 62, dm[4]);

  u8g.drawStr( 90, 10, dr[1]);
  u8g.drawStr( 90, 27, dr[2]);
  u8g.drawStr( 90, 45, dr[3]);
  u8g.drawStr( 90, 62, dr[4]);  
  } while( u8g.nextPage() ); 
}
//=================================================
void NB_printFloat(float value, int places) 
//=================================================
{
  int digit;
  float tens = 0.1;
  int tenscount = 0;
  int i;
  float tempfloat = value;
  float d = 0.5;
  
  if (value < 0) d *= -1.0;
  for (i = 0; i < places; i++) d/= 10.0;    
  tempfloat +=  d;
  if (value < 0) tempfloat *= -1.0;
  while ((tens * 10.0) <= tempfloat) 
  {
    tens *= 10.0;
    tenscount += 1;
  }
  if (value < 0) Serial.print('-');
  if (tenscount == 0) Serial.print(0, DEC);
  for (i=0; i< tenscount; i++) 
  {
    digit = (int) (tempfloat/tens);
    Serial.print(digit, DEC);
    tempfloat = tempfloat - ((float)digit * tens);
    tens /= 10.0;
  }
  if (places <= 0) return;
  Serial.print('.');  
  for (i = 0; i < places; i++) 
  {
    tempfloat *= 10.0; 
    digit = (int) tempfloat;
    Serial.print(digit,DEC);  
    tempfloat = tempfloat - (float) digit; 
  }
}

//=================================================
void NB_sendFloatData(int sid, float value)
//=================================================
{
    digitalWrite(PIN_LED_STATUS,HIGH);
    Serial.print(NABTON_DATA);Serial.print(" "); 
    Serial.print(sid);Serial.print(" "); 
    Serial.print(NFLOAT);Serial.print(" "); 
    NB_printFloat(value,NFLOAT);
    Serial.print(";");
    digitalWrite(PIN_LED_STATUS,LOW);
    //strcpy(dl[3],p);
} 
//=================================================
void setup()
//================================================= 
{
  int i;
  Serial.begin(9600);
  pinMode(PIN_LED_STATUS, OUTPUT);
  NB_oledInit(); 
  sensors.begin();
  nsensors = sensors.getDeviceCount();
  if(nsensors > 0)
  {
    for(i=0;i<nsensors;i++)
    {
      sensors.getAddress(device[i], i);
      sensors.setResolution(device[i], TEMPERATURE_PRECISION);
    }
  }
  NB_oledDraw();
}

//=================================================
void loop()
//=================================================
{
  int i,sid;
  float tempC;
  String str;
  sensors.requestTemperatures();
  NB_oledClear();
  for(i=1;i<=nsensors;i++)
  {  
       sid = g_sids[i];
       tempC = sensors.getTempC(device[i-1]);    
       str = String(tempC);
       str.toCharArray(dl[i],8); 
       NB_sendFloatData(sid,tempC);  
  }
  NB_oledDraw();
  delay(5000);

}

